package com.wuliu.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.common.core.domain.entity.SysUser;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.ThreadLocalContext;
import com.wuliu.system.domain.TspOrder;
import com.wuliu.system.mapper.TspOrderMapper;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.TspTrackMapper;
import com.wuliu.system.domain.TspTrack;
import com.wuliu.system.service.ITspTrackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 路由Service业务层处理
 *
 * @author realue
 * @date 2023-10-24
 */
@Service
public class TspTrackServiceImpl extends ServiceImpl<TspTrackMapper, TspTrack> implements ITspTrackService {
    @Resource
    private TspTrackMapper tspTrackMapper;
    @Resource
    private TspOrderMapper tspOrderMapper;

    /**
     * 查询路由
     *
     * @param id 路由主键
     * @return 路由
     */
    @Override
    public TspTrack selectTspTrackById(String id) {
        return tspTrackMapper.selectTspTrackById(id);
    }

    /**
     * 查询路由列表
     *
     * @param tspTrack 路由
     * @return 路由
     */
    @Override
    public List<TspTrack> selectTspTrackList(TspTrack tspTrack) {
        return tspTrackMapper.selectTspTrackList(tspTrack);
    }

    /**
     * 新增路由
     *
     * @param tspTrack 路由
     * @return 结果
     */
    @Override
    public int insertTspTrack(TspTrack tspTrack) {
        tspTrack.setCreateTime(DateUtils.getNowDate());
        return tspTrackMapper.insertTspTrack(tspTrack);
    }

    /**
     * 修改路由
     *
     * @param tspTrack 路由
     * @return 结果
     */
    @Override
    public int updateTspTrack(TspTrack tspTrack) {
        tspTrack.setUpdateTime(DateUtils.getNowDate());
        return tspTrackMapper.updateTspTrack(tspTrack);
    }

    /**
     * 批量删除路由
     *
     * @param ids 需要删除的路由主键
     * @return 结果
     */
    @Override
    public int deleteTspTrackByIds(String[] ids) {
        return tspTrackMapper.deleteTspTrackByIds(ids);
    }

    /**
     * 删除路由信息
     *
     * @param id 路由主键
     * @return 结果
     */
    @Override
    public int deleteTspTrackById(String id) {
        return tspTrackMapper.deleteTspTrackById(id);
    }

    /**
     * @param orderNo
     * @return
     */
    @Override
    public List<TspTrack> getTracks(String orderNo) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        long count = tspOrderMapper.selectCount(new LambdaQueryWrapper<TspOrder>()
                .eq(TspOrder::getSysAccount, user.getSysAccount())
                .eq(TspOrder::getOrderNo, orderNo));
        if (count <= 0) {
            throw new ServiceException("运单号不存在,请重新输入");
        }
        return tspTrackMapper.selectList(new LambdaQueryWrapper<TspTrack>()
                .eq(TspTrack::getSysAccount, user.getSysAccount())
                .eq(TspTrack::getOrderNo, orderNo)
                .orderByDesc(TspTrack::getOpTime));
    }
}
